package com.mz.mattress.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mz.mattress.admin.mapper.AdvertMapper;
import com.mz.mattress.admin.service.AdvertService;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.Advert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Slf4j
@Service
public class AdvertServiceImpl implements AdvertService {

    @Autowired
    private AdvertMapper advertMapper;

    @Override
    public Result<List<Advert>> list(Integer type) {
        List<Advert> list = advertMapper.selectByType(type);
        return Result.ok(list);
    }

    @Override
    public Result<IPage<Advert>> listPage(Integer type, Integer status, Integer pageNo, Integer pageSize) {
        Page page = new Page(pageNo, pageSize);
        Page<Advert> listPage = advertMapper.listPage(page, type, status);
        return Result.ok(listPage);
    }

    @Override
    public Result<Advert> getById(Integer id) {
        Advert advert = advertMapper.selectById(id);
        if (advert == null) {
            return Result.fail("找不到广告");
        }
        return Result.ok(advert);
    }

    @Override
    public Result add(Advert advert) {
        advertMapper.insert(advert);
        return Result.ok();
    }

    @Override
    public Result updateById(Advert advert) {
        advertMapper.updateById(advert);
        return Result.ok();
    }

    @Override
    public Result deleteByIds(Set<Integer> ids) {
        advertMapper.deleteBatchIds(ids);
        return Result.ok();
    }
}
